package com.example.mntz.service.Impl;

import com.example.mntz.dao.MyInvestmentMapper;
import com.example.mntz.dao.TzHttpUrlMapper;
import com.example.mntz.dao.TzjlMapper;
import com.example.mntz.model.*;
import com.example.mntz.service.TzApiService;
import com.example.mntz.util.TzUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;

@Service
public class TzApiServiceImpl implements TzApiService {
    @Autowired
    private TzHttpUrlMapper tzHttpUrl;
    @Autowired
    private TzjlMapper tzjlMapper;
    @Autowired
    MyInvestmentMapper investmentMapper;
    /*显示各类当前价格*/
    public TzXk contextLoads(int lb, String code)  {
        TzHttpUrl httpUrl = tzHttpUrl.selectTzHttpUrl(lb);
        TzXk tzXk = null;
        try {
            String var = TzUtil.TzUtilGet(code,httpUrl);
            if(lb == 1){/*股票*/
                tzXk = TzUtil.GpTzXk(var);
                tzXk.setCode(code);
            }else if(lb==2){
                tzXk = TzUtil.JjTzXk(var);
                tzXk.setCode(code);
            }else if(lb==3){
                String var1 = TzUtil.TzUtilGet("1",httpUrl);
                tzXk = TzUtil.JzTzXk(var1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return tzXk;
    }
    /*搜索zh*/
    @Override
    public List<Zq> selectZqs(int uId) {
        List<Zq> zqs = investmentMapper.selectZh(uId);
        return zqs;
    }
    /*搜索已有份额*/
    @Override
    public Double SelectTzjl(Tzjl tzjl) {
        Double d = tzjlMapper.selectTzjl(tzjl);
        return d;
    }
    /*买入*/
    @Transactional
    @Override
    public void mrTz(Tzjl tzjl) {
        Tzjl tzjl1 = selectZq(tzjl);
        /*投资明细*/
        tzjl.setReserve1(1);
        tzjlMapper.mrTzmx(tzjl);
        /*账户修改*/
        tzjlMapper.mrUpdateZh(tzjl);
        if(tzjl1==null ){
            tzjlMapper.insert(tzjl);
        }
        else {
            /*投资记录*/
            tzjl.setTzAmount(tzjl1.getTzAmount()+tzjl.getTzAmount());
            tzjl.setResult((tzjl1.getResult()));
            tzjl.setTzFe(tzjl1.getTzFe()+tzjl.getTzFe());
            tzjlMapper.mrTzjl(tzjl);
        }
    }
    /*卖出*/
    @Transactional
    @Override
    public void mcTz(Tzjl tzjl) {
        Tzjl tzjl1 = selectZq(tzjl);
        /*投资明细*/
        tzjl.setReserve1(2);
        tzjlMapper.mrTzmx(tzjl);
        /*账户修改*/
        tzjlMapper.mcUpdateZh(tzjl);
        /*投资记录*/
        tzjl.setResult((tzjl1.getResult()+tzjl.getTzAmount()));
        tzjl.setTzAmount(tzjl1.getTzAmount());
        tzjl.setTzFe(tzjl1.getTzFe()-tzjl.getTzFe());
        tzjlMapper.mrTzjl(tzjl);
    }
    /*收藏投资*/
    @Override
    public void Sctz(Tzjl tzjl) {
        int s = tzjlMapper.selectsctz(tzjl);
        if(tzjl.getTzCode()!=null&&!"".equals(tzjl.getTzCode())) {
            if (s == 0) {
                tzjlMapper.instersctz(tzjl);
            }else {
                tzjlMapper.updatesctz(tzjl);
            }
        }
    }

    @Override
    public LayuiTable selectTzmx(Integer limit, Integer page, Integer zqId, Integer uId) {
        Page page1 = PageHelper.startPage(page,limit);
        List<Tzmx> lx = tzjlMapper.selectTzmx(zqId,uId);
        LayuiTable<Tzmx> layuiTable = new LayuiTable<>();
        layuiTable.setData(lx);
        layuiTable.setCount(page1.getTotal());
        return layuiTable;
    }

    @Override
    public LayuiTable selectTzqk(Integer limit, Integer page, Integer zqId, Integer uId) {
        Page page1 = PageHelper.startPage(page,limit);
        List<Tzmx> lx = tzjlMapper.selectTzqk(zqId,uId);
        LayuiTable<Tzmx> layuiTable = new LayuiTable<>();
        layuiTable.setData(lx);
        layuiTable.setCount(page1.getTotal());
        return layuiTable;
    }

    public Tzjl selectZq(Tzjl tzjl) {
        Tzjl zqs = tzjlMapper.selectOldTzjl(tzjl);
        return zqs;
    }
}
